using System;
using System.Collections.Generic;
using ProjectSurvivor;
using UnityEngine;
using QFramework;
using Random = UnityEngine.Random;

namespace ProjectSurvivor
{
	public partial class RotateSword : ViewController
	{
		    List<Collider2D> mSwards = new List<Collider2D>();
    float radius = Global.RotateSwordRange.Value;
    void Start()
    {
        // Code Here
        void CreateSward()
        {
            mSwards.Add(Sword.InstantiateWithParent(this)
                    .Self(self =>
                    {
                        self.OnTriggerEnter2DEvent((collider) =>
                        {
                            var hurtBox = collider.GetComponent<HitHurtBox>();
                            if (hurtBox)
                            {
                                if (hurtBox.Owner.CompareTag("Enemy") || hurtBox.Owner.CompareTag("EnemyFly"))
                                {
                                    var damageTimes = Global.SuperRotateSward.Value ? Random.Range(2, 3 + 1) : 1;
                                    DamageSystem.CalculateDamage(Global.RotateSwordDamage.Value * damageTimes, hurtBox.Owner.GetComponent<IEnemy>());
                                    if (UnityEngine.Random.Range(0, 1.0f) < 0.5f)
                                    {
                                        collider.attachedRigidbody.velocity = collider.NormalizedDirection2DFrom(self) * 5 + collider.NormalizedDirection2DFrom(Player.Default) * 10;
                                    }
                                }
                            }
                        }).UnRegisterWhenGameObjectDestroyed(self);
                    })
                    .Show());
        }

        void CreateSwards()
        {
            var toaddCount = Global.RotateSwordCount.Value + Global.AdditionFlyThingCount.Value - mSwards.Count;
            for (global::System.Int32 i = 0; i < toaddCount; i++)
            {
                CreateSward();
            }
            UpDateCirclePos();
        }

        Global.RotateSwordCount.Or(Global.AdditionFlyThingCount).Register(() =>
        {
            CreateSwards();

            
        }).UnRegisterWhenGameObjectDestroyed(gameObject);
        CreateSwards();
        Global.RotateSwordRange.RegisterWithInitValue((range) =>
        {
            UpDateCirclePos();
        });
        Sword.Hide();

       
    }

    void UpDateCirclePos()
    {
        var durationDegrees = 360 / mSwards.Count;
        for (int i = 0; i < mSwards.Count; i++)
        {
            var circleLocalPos = new Vector2(Mathf.Cos(durationDegrees * i * Mathf.Deg2Rad), Mathf.Sin(durationDegrees * i * Mathf.Deg2Rad)) * radius;
            mSwards[i].LocalPosition(circleLocalPos.x, circleLocalPos.y).LocalEulerAnglesZ(durationDegrees * i - 90);
        }

    }
    private void Update()
    {
        var speedTimes = Global.SuperRotateSward.Value ? Time.frameCount * 10 : Global.RotateSwordSpeed.Value * Time.frameCount;

        this.LocalEulerAnglesZ(-speedTimes);

    }
	    }
}
